var addWorkModel;
var updateWorkModel;
(function(){
    var form = layui.form;
    var table= layui.table;

    form.on('submit(addWorkBtn)', function (data) {
        var $ = layui.$;
        var workName = data.field.WorkName;
        var workSex = data.field.WorkSex;
        var workAddr=data.field.WorkAddr;
        var workNum=data.field.WorkNum;
        $.ajax({
            url: "../work/insert",
            method: "post",
            data: {
                "WorkName": workName,
                "WorkSex": workSex,
                "WorkAddr":workAddr,
                "WorkNum":workNum
            },
            success: function (data) {
                if (data.code == 200) {
                    layui.layer.close(addWorkModel);
                    layui.layer.msg(data.msg,{icon:1});
                    setTimeout(function () {
                    },1000)
                    layui.table.reload("demo");
                } else if (data.code == 500) {
                    layui.layer.msg(data.msg, {icon: 2});
                    setTimeout(function () {
                    },1000)
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
        return false;
    });
    form.on('submit(updateWorkBtn)', function (data) {

        var workId = layui.$("#editWorkForm input[name='workId']").val();
        var workName = data.field.workName;
        var workSex = data.field.workSex;
        var workAddr = data.field.workAddr;
        var workNum = data.field.workNum;
        layui.$.ajax({
            url: "../work/updateWork",
            method: "put",
            data: {
                "workId":workId,
                "workName":workName,
                "workSex":workSex,
                "workAddr":workAddr,
                "workNum":workNum
            },
            success: function (data) {
                if (data.code == 200) {
                    layui.layer.close(updateWorkModel);
                    layui.table.reload("demo");

                } else if (data.code == 500) {
                    layui.layer.msg(data.msg, {icon: 2});
                    console.log(data)

                }
            },
            error: function (data) {
                console.log(data);
            }
        });
        return false;
    });


    table.render({
        elem: '#demo'
        ,url: '../work/findByPage' //数据接口
        ,height:500
        ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'workId', title: 'Id', width:100, sort: true, fixed: 'left'}
            ,{field: 'workName', title: '职工姓名', width:90}
            ,{field: 'workSex', title: '性别', width:60}
            ,{field: 'workAddr', title: '住址', width:220}
            ,{field: 'workNum', title: '联系方式', width:160}
            ,{field: 'updateTime', title: '更新时间', width: 210}
        ]]
        ,limit: 10
        ,page: true
        ,response: {
            statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
        }
        ,parseData: function(res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },where:{
            "workName":layui.$("input[name='searchWorkName']").val()
        }
    });
})();
function clearForm() {
    var $ = layui.$;
    $("input[name='WorkName'] ").val("");
    $("input[name='WorkSex'] ").val("");
    $("input[name='WorkAddr'] ").val("");
    $("input[name='WorkNum'] ").val("");
}
function showWorkModel() {
    clearForm();
    var layer=layui.layer;
    var $ =layui.$;
    addWorkModel =layer.open({
        type:1,
        title:"添加员工信息",
        content:$("#addWorkModel"),
    });
}
function editWork() {
//获取被选中项
    var checkStatus = layui.table.checkStatus('demo');
    if (checkStatus.data.length ==0){
        layui.layer.msg("请选择数据",{icon:2})
        setTimeout(function () {
        },1000)
        return;
    }
    if(checkStatus.data.length >1){
        layui.layer.msg("请选择一条数据",{icon:2});
        setTimeout(function () {
        },1000)

        return;
    }
    var workId = checkStatus.data[0].workId;
    //从后台加载数据
    layui.$.ajax({
        url:"../work/work/"+workId,
        method:"get",
        success:function (result) {
            console.log(result.data);
            //填充到编辑框中
            layui.$("#editWorkModel input[name='workId']").val(result.data.workId);
            layui.$("#editWorkModel input[name='workName']").val(result.data.workName);
            layui.$("#editWorkModel input[name='workSex']").val(result.data.workSex);
            layui.$("#editWorkModel input[name='workAddr']").val(result.data.workAddr);
            layui.$("#editWorkModel input[name='workNum']").val(result.data.workNum);
            //展示编辑框
            updateWorkModel = layui.layer.open({
                type: 1,
                title: "编辑用户信息",
                content: layui.$("#editWorkModel")
            });
            //点击确定提交

        }
    });
}

function deleteWork(){
    //获取被选中项
    //通过Ajax 发起异步删除请求
    var checkStatus = layui.table.checkStatus('demo'); //idTest 即为基础参数 id 对应的值
    if(checkStatus.data.length==0){
        layui.layer.msg("请选择数据",{icon:2});
        setTimeout(function () {
        },1000)
        return;
    }
    console.log(checkStatus.data) //获取选中行的数据
    var workIds=new Array();
    for (let i=0 ;i<checkStatus.data.length; i++ ){
        workIds.push(checkStatus.data[i].workId);
    }
    layui.$.ajax({
        url:"../work/batchDelete?workIds="+workIds.join(),
        method: "delete",
        success:function (data) {
            layui.layer.msg(data.msg,{icon:1});
            setTimeout(function () {
            },1000)
            layui.table.reload("demo");
        },error: function(data){
        }
    })
}
function searchWork(){
    layui.table.reload("demo",{
        where:{
            "workName":layui.$("input[name='searchWorkName']").val()
        }
    });
}